/*
 * Created on 2005/05/10
 * Author aki@www.xucker.jpn.org
 * License Apache2.0 or Common Public License
 */
package jp.sourceforge.akjrcp.ui.draw2d;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.swt.graphics.Image;


/**
 * 
 *
 */
public class ScalableImageFigure extends DoubleScalableFigure{
    public static Log log = LogFactory.getLog(ScalableImageFigure.class);
    
    private CachedImageLoader cachedImageLoader;
    private String key;
    public ScalableImageFigure(CachedImageLoader cachedImageLoader,String key,int defaultWidth,int defaultHeight){
        super(defaultWidth,defaultHeight);
        this.cachedImageLoader=cachedImageLoader;
        this.key=key;
        resize();
    }
    
    
    public Image getImage(){
        return cachedImageLoader.loadImage(key);
    }
    
   
   
 
    
    
    public void paintFigure(Graphics g) {
        //log.infopaint:"+key);
        //log.info(this.getClientArea());
        log.trace("paint:"+getClientArea()+" key="+key);
        Image img=cachedImageLoader.loadImage(key);
        if(img!=null && !img.isDisposed()){
            //change resize.
            int w=img.getBounds().width;
            int h=img.getBounds().height;
            if(w!=defaultWidth || h!=defaultHeight){
                defaultWidth=w;
                defaultHeight=h;
                resize();
                getParent().revalidate();//resized.
                
            }
            //clientarea is width & height.
            g.drawImage(img,0,0,w,h,getClientArea().x,getClientArea().y,this.getClientArea().width,this.getClientArea().height);
        	}
        }
    
}
